package com.suneast.suneaststore.modules.personal.wallet;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.personal.wallet.WeChatPayBean;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.personal.wallet.event.GotWeChatTopUpData;
import com.suneast.suneaststore.modules.wechat.LatteWeChat;
import com.suneast.suneaststore.ui.customview.MoneyTextWatcher;
import com.suneast.suneaststore.ui.fragments.BaseFragment;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class TopUpFragment extends BaseFragment {
    @Override
    public int setLayout() {
        return R.layout.fragment_top_up;
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_total_value)
    TextView balanceValueTv;
    @BindView(R.id.cb_wechat_pay)
    CheckBox wechatPayCb;
    @BindView(R.id.cb_ali_pay)
    CheckBox aliPayCb;
    @BindView(R.id.et_top_up_value)
    AppCompatEditText topUpValueEt;
    @BindView(R.id.btn_top_up)
    Button topUpBtn;
    @BindView(R.id.rv_top_up_trace)
    RecyclerView topUpTraceRv;

    private double balanceValue = 0.00;

    public TopUpFragment withBalanceValue(double value) {
        balanceValue = value;
        return this;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        initView();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    private @Flag.PayType
    int payType = Flag.PayType.WECHAT_PAY;

    private void initView() {
        toolbar.setNavigationOnClickListener(v -> pop());
        DecimalFormat df = new DecimalFormat("0.00");
        balanceValueTv.setText(df.format(balanceValue));
        wechatPayCb.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
            if (isChecked) {
                payType = Flag.PayType.WECHAT_PAY;
                aliPayCb.setChecked(false);
            }
        });
        aliPayCb.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
            if (isChecked) {
                payType = Flag.PayType.ALI_PAY;
                wechatPayCb.setChecked(false);
            }
        });
        topUpValueEt.addTextChangedListener(new MoneyTextWatcher(topUpValueEt));
    }

    @OnClick({R.id.btn_top_up})
    public void onViewClick(View view) {
        if (view.getId() == R.id.btn_top_up) {
            //根据支付方式 将金额作为参数,传给后台
            if (payType == Flag.PayType.WECHAT_PAY) {
                String topUpStr = topUpValueEt.getText().toString();
                if (topUpStr.length() > 0) {
                    double value = Double.valueOf(topUpStr);
                    if (value > 0d) {
                        WalletHandler.rechargeByWechatPay(value);
                        topUpBtn.setEnabled(false);
                    }
                } else {
                    Toast.makeText(_mActivity, "请输入充值金额", Toast.LENGTH_SHORT).show();
                }
            } else if (payType == Flag.PayType.ALI_PAY) {

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotWeChatTopUpData(GotWeChatTopUpData data) {
        if (data.getResult() == Flag.Result.SUCCEED) {
            LatteLogger.e("test", "GotWeChatTopUpData SUCCEED");
            WeChatPayBean payInfoBean = data.getDataBean();
            PayReq request = new PayReq();
            request.appId = payInfoBean.getAppid();
            request.partnerId = payInfoBean.getPartnerid();
            request.prepayId = payInfoBean.getPrepayid();
            request.packageValue = payInfoBean.getPackageX();
            request.nonceStr = payInfoBean.getNoncestr();
            request.timeStamp = String.valueOf(payInfoBean.getTimestamp());
            request.sign = payInfoBean.getSign();
            request.extData = "topup";
            IWXAPI api = LatteWeChat.getInstance().getWXAPI();
            api.sendReq(request);
            pop();
        } else {
            topUpBtn.setEnabled(true);
            Toast.makeText(_mActivity, "请求充值失败", Toast.LENGTH_SHORT).show();
        }
    }
}
